<?php
// this is not a PHPUnit test!!!!!!
// this is not a PHPUnit test!!!!!!
// this is not a PHPUnit test!!!!!!
// this is not a PHPUnit test!!!!!!
// this is not a PHPUnit test!!!!!!
// this is not a PHPUnit test!!!!!!

require_once(dirname(__FILE__)."/../../../autoload.php");

Conf::set( 'DB_HOST' , 'localhost' );
Conf::set( 'DB_NAME' , 'test_db' );
Conf::set( 'DB_USER' , 'test' );
Conf::set( 'DB_PASS' , 'test' );

Conf::set( 'SESSION_NS' , 'test' );

$sql = "CREATE TABLE IF NOT EXISTS `test_session` (
  `n_id` int(10) unsigned NOT NULL auto_increment,
  `sess_id` varchar(255) NOT NULL,
  `data_str` text NOT NULL,
  `time_stamp` int(11) NOT NULL,
  PRIMARY KEY  (`n_id`),
  UNIQUE KEY `sess_id` (`sess_id`),
  KEY `time_stamp` (`time_stamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";

DBAdapter::query($sql , DBAdapter::NONE);

$sh = new DBSessionHandler();
		
session_set_save_handler(  
 	  array($sh,"open"),  
 	 	array($sh,"close"),  
   	array($sh,"read"),  
   	array($sh,"write"),  
   	array($sh,"destroy"),  
   	array($sh,"gc")
);
		


$res = DBAdapter::query("SELECT sess_id FROM test_session ORDER BY n_id DESC LIMIT 1" , DBAdapter::ASSOC);



session_id($res[0]['sess_id']);
session_start();

echo "SessionID: " . $res[0]['sess_id']. "\n";

$inArr = explode( "\n", file_get_contents("data.txt"));

if (is_array($inArr)) {
	foreach( $inArr as $line ) {
		$cols = explode(",", $line);
		if (!is_array($cols)) continue;
		$key = $cols[0];
		$val = $cols[1];
		if ( strcmp($_SESSION[$key], $val) != 0 ) die("Error: [" . $_SESSION[$key] . "] (key=".$key.") != [".$val."]. Line: ". __LINE__."\n");
	}
}

echo "All session variables match\n";

$sh->gc(0);
$res_gc = DBAdapter::query("SELECT count(*) c FROM test_session " , DBAdapter::ASSOC);
if ($res_gc[0]['c'] != 0) die("gc failed. Line: ". __LINE__. ".\n");


//DBAdapter::query("DROP TABLE IF EXISTS `test_session`;" , DBAdapter::NONE);


